package com.qidian.mes.cal.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.mes.cal.mapper.CalPlanMapper;
import com.qidian.mes.cal.service.CalPlanService;
import com.qidian.mes.pojo.cal.CalPlan;
import com.qidian.mes.pojo.cal.CalTeam;
import com.qidian.mes.pojo.cal.CalTeamMember;
import com.qidian.mes.util.pojo.MESResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 04冯虹丹
 * @Company 起点编程
 * @Time 2023/10/31 11:40
 * @Description
 */
@Service
@Transactional
public class CalPlanServiceImpl implements CalPlanService {

    @Autowired
    private CalPlanMapper calPlanMapper;

    /**
     * 分页查询
     * @param page
     * @param limit
     * @param calPlan
     * @return
     */
    @Override
    public MESResult list(int page, int limit, CalPlan calPlan) {
        PageHelper.startPage(page,limit);
        List<CalPlan> list = calPlanMapper.list(calPlan);
        PageInfo<CalPlan> pageInfo = new PageInfo<>(list);
        return MESResult.ok(pageInfo);
    }

    /**
     * 单个删除
     * @param planId
     * @return
     */
    @Override
    public MESResult delete(Long planId) {
        return MESResult.updateIsOk(calPlanMapper.delete(planId));
    }

    /**
     * 批量删除
     * @param planId
     * @return
     */
    @Override
    public MESResult deletes(Long[] planId) {
        int count = calPlanMapper.deletes(planId);
        return MESResult.ok(count + "");
    }

    /**
     * 添加排班计划
     * @param calPlan
     * @return
     */
    @Override
    public MESResult save(CalPlan calPlan) {
        if(calPlanMapper.selectByCalPlanName(calPlan.getPlanName()) != null || calPlanMapper.selectByCalPlanCode(calPlan.getPlanCode()) != null){
            return MESResult.duplicate("计划编号或计划名称重复");
        }
        return MESResult.updateIsOk(calPlanMapper.save(calPlan));
    }

    /**
     * 编辑排班计划
     * @param calPlan
     * @return
     */
    @Override
    public MESResult update(CalPlan calPlan) {
        CalPlan calPlanTemp = calPlanMapper.selectByCalPlanName(calPlan.getPlanName());
        CalPlan calPlanCode = calPlanMapper.selectByCalPlanCode(calPlan.getPlanCode());
        if((calPlanTemp != null && !calPlanTemp.getPlanId().equals(calPlan.getPlanId())) || (calPlanCode != null && !calPlanCode.getPlanId().equals(calPlan.getPlanId()) )){
            return MESResult.duplicate("计划编号或计划名称重复");
        }
        return MESResult.updateIsOk(calPlanMapper.update(calPlan));
    }

    /**
     * 根据Id查询
     * @param planId
     * @return
     */
    @Override
    public MESResult get(Long planId) {
        CalPlan calPlan = null;
        if((calPlan = calPlanMapper.get(planId)) != null){
            return MESResult.ok(calPlan);
        }
        return MESResult.empty();
    }
}
